package com.chiancloud.shield.server.services.workflow;

import org.apache.hadoop.service.AbstractService;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * Created by wanghui on 14-8-20.
 */
public abstract class AbstractWorkflowExecutorService extends AbstractService {
    private ExecutorService executor;
    public AbstractWorkflowExecutorService(String name) {
        this(name, null);
    }
    protected AbstractWorkflowExecutorService(String name,
                                              ExecutorService executor) {
        super(name);
        this.executor = executor;
    }
    public synchronized ExecutorService getExecutor() {
        return executor;
    }

    protected synchronized void setExecutor(ExecutorService executor) {
        this.executor = executor;
    }

    public void execute(Runnable runnable) {
        getExecutor().execute(runnable);
    }


    public <V> Future<V> submit(Callable<V> callable) {
        return getExecutor().submit(callable);
    }
    @Override
    protected void serviceStop() throws Exception {
        super.serviceStop();
        stopExecutor();
    }
    protected synchronized void stopExecutor() {
        if (executor != null) {
            executor.shutdownNow();
        }
    }

}
